﻿

@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "库区管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var warehouses = ViewBag.Warehouses as List<IOA.MES.DataAccess.VModel.VWarehouse>;
}
@model List<IOA.MES.DataAccess.VModel.VWorkshop>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">选择仓库：</label>
                    <div class="layui-input-inline">
                        <select id="selWarehouses" lay-filter="selWarehouses">
                            <option value="">全部仓库</option>
                            @foreach (var item in warehouses)
                            {
                                <option value="@item.WarehouseCode">@($"{item.WarehouseName}-{item.WarehouseCode}")</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">关键字：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="txtKey" class="layui-input" value="" placeholder="请输入库区代码或库区名称" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">库区状态：</label>
                    <div class="layui-input-inline">
                        <select id="selStatus" lay-filter="selStatus">
                            <option value="-1">所有状态</option>
                            <option value="@((int)CommonStatus.Normal)" selected>正常</option>
                            <option value="@((int)CommonStatus.Disabled)">禁用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editWarehouseArea(0);">
                        <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card">
        <div class="layui-card-body">
            <table id="table" lay-filter="table"></table>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        initLayui();
        function initLayui() {
            layui.use(['table', 'layer', 'laydate', 'form'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    laydate = layui.laydate,
                    form = layui.form;

                var options = {
                    elem: '#table'
                    , url: '/Warehouse/QueryWarehouseAreas'
                    , method: "post"
                    , where: { warehouseCode: '', key: '', status: 1 }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60 }
                        , {
                            title: '仓库', templet: function (d) {
                                return d.WarehouseName + "-" + d.WarehouseCode
                            }
                        }
                        , { field: 'AreaCode', title: '库区代码' }
                        , { field: 'AreaName', title: '库区名称' }
                        , { field: 'CNStatus', title: '库区状态' }
                        , { field: 'CreateTimeStr', title: '创建时间' }
                        , {
                            title: '操作', width: 200, templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="editWarehouseArea(' + d.PkId + ')"><i class="layui-icon">&#xe615;</i>查看</button>\
                                               <button class="layui-btn layui-btn-sm" onclick="modifyStatus(' + d.PkId + ',' + (1 - d.Status) + ')"><i class="layui-icon">&#xe616;</i>' + (d.Status == 0 ? "启用" : "禁用") + '</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    var status = parseInt($("#selStatus").val());

                    options.where.warehouseCode = $("#selWarehouses").val().trim();
                    options.where.status = status == -1 ? null : status;
                    options.where.key = $("#txtKey").val().trim();

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function editWarehouseArea(areaId) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '350px'],
                content: "/Warehouse/WarehouseAreaEdit?areaId=" + areaId,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function modifyStatus(areaId, status) {
            var loadId = layer.load();
            $.ajax({
                url: "/Warehouse/ModifyWarehouseAreaStatus",
                data: {
                    areaId: areaId,
                    status: status
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    layer.msg("修改成功！", { icon: 6, time: 1500 });
                    $("#btnSearch").click();
                }
            });
        }
    </script>
}
